
await @promise("slime.ready");
@lab.export.service("slime_test@", "./service");
@lab.export.service("slime_test",function (lab) {
    return {
        doAction : function (session, name, params ) {
            return lab.doAction(name,params);
        },
        doQuery : function (session, {start , limit, models, conditions = [], params = [], fields = ["*"], sort = "", group = "" }) {
            return lab.query(models,{
                start , limit, conditions, params, fields, sort, group
            })
        },
        doFetch : function (session, {model, condition = {}, start = 0 , limit = -1}) {
            return lab.sqlite.find(model, condition , limit < 0 ? undefined : limit, limit < 0 ? undefined : start).then(function ({records}) {
                this.next(records);
            });
        },
        doImport : function (session, model, data) {
            if (lab.models[model]) {
                return @.async(function () {
                    if (data.id){
                        lab.find(model,data.id).pipe(this);
                    } else {
                        this.next();
                    }
                }).then(function (exist_data) {
                    @dump(data);
                    if (exist_data) {
                        return lab.update(model, exist_data, data);
                    } else {
                        return lab.create(model, data);
                    }
                });
            } else {
                throw @lab.errors.not_support;
            }
        },
        doSmartImport : function (session, model, data) {
            if (!session.currentOU){
                throw @lab.errors.not_login;
            }
            if (lab.models[model]) {
                return @.async(function () {
                    if (data.id){
                        lab.find(model,data.id).pipe(this);
                    } else {
                        this.next();
                    }
                }).then(function (exist_data) {
                    @dump(data);
                    if (exist_data) {
                        return lab.updateWithoutCache(model, exist_data, data,{
                            ouid : session.currentOU.id
                        });
                    } else {
                        return lab.create(model, @.merge.simple(data,{
                            ouid : session.currentOU.id
                        }));
                    }
                });
            } else {
                throw @lab.errors.not_support;
            }
        },
        doFetchByJSONRPC : function (session, {start , limit, model, conditions = {} , jsonrpc = "jsonrpc"}) {
            return lab[jsonrpc].slime_test.doFetch({start , limit, model, conditions });
        },
        doSmartImportByJSONRPC : function (session, {model, conditions  = {}, jsonrpc = "jsonrpc"}) {
            if (!session.currentOU){
                throw @lab.errors.not_login;
            }
            var syncData = [];
            var start = 0;
            var limit = 50;
            return @.async(function () {
                lab.slime_test.doFetchByJSONRPC(session, {
                    start , limit, model, jsonrpc, conditions
                }).finished((error,list) => {
                    if (error) {
                        this.reject(error);
                    } else if (list.length){
                        start += limit;
                        syncData = syncData.concat(list);
                        this.retry();
                    } else {
                        this.next(syncData);
                    }
                })
            }).all(function (remoteData) {
                lab.slime_test.doSmartImport(session, model, remoteData).pipe(this);
            })
        }
    }    
})